from flask import Blueprint, render_template, jsonify, redirect, url_for
from flask import request
from flask_mail import Message
from models import User
from exts import mail, db

bp = Blueprint('user', __name__, url_prefix='/')

@bp.route('/register')
def register():
    return render_template('register.html')
@bp.route('/user/add',methods=['POST'])
def add_user():
    user = User(username=request.form.get('username'), password=request.form.get('password'))
    db.session.add(user)
    db.session.commit()
    return render_template('useradd.html')

@bp.route('/login/check', methods=['POST'])
def login_check():
    username = request.form.get('username')
    password = request.form.get('password')
    users = User.query.filter_by(username= username)
    if users.count() != 0:
        for user in users:
            if user.username == username and user.password == password:
                return render_template("index.html")
            else:
                return render_template('login.html')
    else:
        return render_template('login.html')

# @bp.route('/captcha/email')
# def get_captcha_email():
#     email = request.args.get('email')
#     source = string.digits * 4
#     captcha = random.sample(source, 4)
#     captcha = "".join(captcha)
#     msg = Message(subject="注册验证码", recipients=[email], body=f"您的验证码是{captcha}")
#     mail.send(msg)
#     email_captcha = EmailCaptchaModel(email = email, captcha = captcha)
#     db.session.add(email_captcha)
#     db.session.commit()
#     return jsonify({"code": 200, "msg": "发送成功"})
# @bp.route('/mail/test')
# def mail_test():
#     msg = Message(subject="测试邮件", recipients=["2055739377@qq.com"], body="这是一封测试邮件")
#     mail.send(msg)
#     return "发送成功"

@bp.route('/function1')
def function1():
    return render_template('function1.html')

@bp.route('/function2')
def function2():
    return render_template('function2.html')

@bp.route('/function3')
def function3():
    return render_template('function3.html')

@bp.route('/function4')
def function4():
    return render_template('function4.html')

@bp.route('/heartrate', methods=['POST'])
def make1():
    heart_rate1 = request.form.get('heart_rate1')
    heart_rate2 = request.form.get('heart_rate2')
    heart_rate3 = request.form.get('heart_rate3')
    heart_rate4 = request.form.get('heart_rate4')
    heart_rate5 = request.form.get('heart_rate5')
    heart_rate6 = request.form.get('heart_rate6')
    heart_rate7 = request.form.get('heart_rate7')
    from pyecharts.charts import Line
    from pyecharts.options import TitleOpts, LegendOpts, ToolboxOpts, VisualMapOpts
    line = Line()
    line.add_xaxis(["第一次心率", "第二次心率","第三次心率","第四次心率","第五次心率","第六次心率","第七次心率"])
    line.add_yaxis("心率（次/分钟）", [heart_rate1, heart_rate2,heart_rate3,heart_rate4,heart_rate5,heart_rate6,heart_rate7])
    line.set_global_opts(
        title_opts=TitleOpts(title="心率可视化", pos_left="center", pos_bottom="1%"),
        legend_opts=LegendOpts(is_show=True),
        toolbox_opts=ToolboxOpts(is_show=True),
        visualmap_opts=VisualMapOpts(is_show=True)
    )
    output_path = 'templates/heartrate.html'
    line.render(output_path)
    return render_template('heartrate.html')

@bp.route('/bloodsugar', methods=['POST'])
def make2():
    bloodsugar1 = request.form.get('bloodsugar1')
    bloodsugar2 = request.form.get('bloodsugar2')
    bloodsugar3 = request.form.get('bloodsugar3')
    bloodsugar4 = request.form.get('bloodsugar4')
    bloodsugar5 = request.form.get('bloodsugar5')
    bloodsugar6 = request.form.get('bloodsugar6')
    bloodsugar7 = request.form.get('bloodsugar7')
    # gender = request.form.get('gender')
    from pyecharts.charts import Line
    from pyecharts.options import TitleOpts, LegendOpts, ToolboxOpts, VisualMapOpts
    line = Line()
    line.add_xaxis(["第一次血糖", "第二次血糖", "第三次血糖", "第四次血糖", "第五次血糖", "第六次血糖", "第七次血糖"])
    line.add_yaxis("血糖（mmol/L）", [bloodsugar1, bloodsugar2, bloodsugar3, bloodsugar4, bloodsugar5, bloodsugar6, bloodsugar7])
    line.set_global_opts(
        title_opts=TitleOpts(title="血糖可视化", pos_left="center", pos_bottom="1%"),
        legend_opts=LegendOpts(is_show=True),
        toolbox_opts=ToolboxOpts(is_show=True),
        visualmap_opts=VisualMapOpts(is_show=True)
    )
    output_path = 'templates/bloodsugar.html'
    line.render(output_path)
    return render_template('bloodsugar.html')

@bp.route('/weight', methods=['POST'])
def make3():
    weight1 = request.form.get('weight1')
    weight2 = request.form.get('weight2')
    weight3 = request.form.get('weight3')
    weight4 = request.form.get('weight4')
    weight5 = request.form.get('weight5')
    weight6 = request.form.get('weight6')
    weight7 = request.form.get('weight7')
    # gender = request.form.get('gender')
    from pyecharts.charts import Line
    from pyecharts.options import TitleOpts, LegendOpts, ToolboxOpts, VisualMapOpts
    line = Line()
    line.add_xaxis(["第一次体重", "第二次体重", "第三次体重", "第四次体重", "第五次体重", "第六次体重", "第七次体重"])
    line.add_yaxis("体重（kg）", [weight1, weight2, weight3, weight4, weight5, weight6, weight7])
    line.set_global_opts(
        title_opts=TitleOpts(title="体重可视化", pos_left="center", pos_bottom="1%"),
        legend_opts=LegendOpts(is_show=True),
        toolbox_opts=ToolboxOpts(is_show=True),
        visualmap_opts=VisualMapOpts(is_show=True)
    )
    output_path = 'templates/weight.html'
    line.render(output_path)
    return render_template('weight.html')

@bp.route('/steps', methods=['POST'])
def make4():
    steps1 = request.form.get('steps1')
    steps2 = request.form.get('steps2')
    steps3 = request.form.get('steps3')
    steps4 = request.form.get('steps4')
    steps5 = request.form.get('steps5')
    steps6 = request.form.get('steps6')
    steps7 = request.form.get('steps7')
    # gender = request.form.get('gender')
    from pyecharts.charts import Line
    from pyecharts.options import TitleOpts, LegendOpts, ToolboxOpts, VisualMapOpts
    line = Line()
    line.add_xaxis(["第一天", "第二天", "第三天", "第四天", "第五天", "第六天", "第七天"])
    line.add_yaxis("运动步数（步）", [steps1, steps2, steps3, steps4, steps5, steps6, steps7])
    line.set_global_opts(
        title_opts=TitleOpts(title="运动步数可视化", pos_left="center", pos_bottom="1%"),
        legend_opts=LegendOpts(is_show=True),
        toolbox_opts=ToolboxOpts(is_show=True),
        visualmap_opts=VisualMapOpts(is_show=True)
    )
    output_path = 'templates/steps.html'
    line.render(output_path)
    return render_template('steps.html')
